alter   function fn_pcm_data ( @processStepId bigint)
returns @results Table(Lot_Number varchar(50),
			WAT varchar(50),
			WSM varchar(50),
			QCI varchar(50))
as
begin
	declare @temp_result Table(Lot_Number varchar(50),
			WAT varchar(50),
			WSM varchar(50),
			QCI varchar(50))
	declare @lot_number varchar(50)
	declare @wat varchar(50)
	declare @wsm varchar(50)
	declare @qci varchar(50)
	declare @pcm_lot_number varchar(50)
	declare tempCursor CURSOR FOR
	select Lot_Table.Lot_Number from Lot_Table where ProcessStep_ID = @processStepId
	
	open tempCursor
		fetch next from tempCursor
		into @lot_number
		while @@fetch_status = 0
		begin
			select @pcm_lot_number = Lot_Number, @wat = Wat_path, @wsm = Wsm_path, @qci = Qci_path from pcm_data where Lot_Number = @lot_number 
			if @pcm_lot_number = @lot_number --and ( @wat is not null or @wsm is not null or @qci is not null )
			begin
				insert into @temp_result values(@pcm_lot_number, @wat, @wsm, @qci)
			end
			set @pcm_lot_number = ''
			set @wat = ''
			set @wsm = ''
			set @qci = ''
			fetch next from tempCursor into @lot_number
		end
	close tempCursor
	deallocate tempCursor
	insert into @results select *from @temp_result
	return
end